package com.food.eat.impl;

import com.food.eat.enums.YesOrNo;
import com.food.eat.mapper.UserAddressMapper;
import com.food.eat.pojo.UserAddress;
import com.food.eat.pojo.bo.AddressBo;
import com.food.eat.service.AddressService;
import org.n3r.idworker.Sid;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author:RenPu
 * @Date: 2020/7/21 10:11
 * @Version: 1.0
 * @description: 收货地址功能的实现
 */

@Service
public class AddressServiceimpl implements AddressService {

    @Resource
    private Sid sid;

    @Resource
    private UserAddressMapper userAddressMapper;


    /**
     * 根据用户的id值，获取所对应的收回地址的信息
     *
     * @param userId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<UserAddress> queryByUserId(String userId) {

        UserAddress userAddress = new UserAddress();
        userAddress.setUserId(userId);
        List<UserAddress> userAddresses = userAddressMapper.select(userAddress);
        return userAddresses;
    }


    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void addUserNewAddress(AddressBo addressBo) {

        //查看当前用户是第一次添加收货地址，如果是则设为默认收货地址

        Integer isDefault = 0;

        List<UserAddress> userAddresses = this.queryByUserId(addressBo.getUserId());
        if (userAddresses == null || userAddresses.size() == 0) {
            isDefault = 1;
        }


        String addressId = sid.nextShort();
        UserAddress userAddress = new UserAddress();
        BeanUtils.copyProperties(addressBo, userAddress);
        userAddress.setId(addressId);
        userAddress.setUpdatedTime(new Date());
        userAddress.setCreatedTime(new Date());
        userAddress.setIsDefault(isDefault);

        //创建新的收货地址,保存在数据库
        userAddressMapper.insert(userAddress);


        //与当前事务的行为，保持一致
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
            @Override
            public void afterCommit() {

            }
        });

    }


    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    @Override
    public void updateUserAddress(AddressBo addressBo) {

        String addressId = addressBo.getAddressId();

        UserAddress userAddress = new UserAddress();
        BeanUtils.copyProperties(addressBo, userAddress);

        //实体的属性名称不一致，无法使用BeanUtils.copyProperties
        userAddress.setId(addressId);
        userAddress.setUpdatedTime(new Date());
        userAddressMapper.updateByPrimaryKeySelective(userAddress);

    }


    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void deleteUserAddress(String userId, String addressId) {

        UserAddress userAddress = new UserAddress();
        userAddress.setUserId(userId);
        userAddress.setId(addressId);

        userAddressMapper.delete(userAddress);
    }


    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void updateUserAddressToDefault(String userId, String addressId) {

        //将当前用户下的所有收货地址，全部设置为非默认

        UserAddress userAddress = new UserAddress();
        userAddress.setUserId(userId);
        List<UserAddress> userAddressList = userAddressMapper.select(userAddress);
        for (UserAddress address : userAddressList) {
            address.setIsDefault(YesOrNo.NO.type);
            userAddressMapper.updateByPrimaryKeySelective(address);
        }

        //根据用户的id值以及收货地址的id值进行设置默认
        UserAddress defaultUserAddress = new UserAddress();
        defaultUserAddress.setUserId(userId);
        defaultUserAddress.setId(addressId);
        defaultUserAddress.setIsDefault(YesOrNo.YES.type);
        userAddressMapper.updateByPrimaryKeySelective(defaultUserAddress);


    }
}
